﻿
#region NameSpace
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using UserManage;
#endregion

public partial class ucAddPermission : System.Web.UI.UserControl
{
    UserManager _userPermission = new UserManager();

    #region Events
    protected void Page_Load(object sender, EventArgs e)
    {
        btnAddPermission.Attributes.Add("onmouseover", "this.style.backgroundColor='#00B5F0'");
        btnAddPermission.Attributes.Add("onmouseout", "this.style.backgroundColor='#008FBF'");
        btnAssign.Attributes.Add("onmouseover", "this.style.backgroundColor='#00B5F0'");
        btnAssign.Attributes.Add("onmouseout", "this.style.backgroundColor='#008FBF'");
        btnBack.Attributes.Add("onmouseover", "this.style.backgroundColor='#00B5F0'");
        btnBack.Attributes.Add("onmouseout", "this.style.backgroundColor='#008FBF'");
        btnDeletePermission.Attributes.Add("onmouseover", "this.style.backgroundColor='#00B5F0'");
        btnDeletePermission.Attributes.Add("onmouseout", "this.style.backgroundColor='#008FBF'");
        btnEditPermission.Attributes.Add("onmouseover", "this.style.backgroundColor='#00B5F0'");
        btnEditPermission.Attributes.Add("onmouseout", "this.style.backgroundColor='#008FBF'");
        btnSave.Attributes.Add("onmouseover", "this.style.backgroundColor='#00B5F0'");
        btnSave.Attributes.Add("onmouseout", "this.style.backgroundColor='#008FBF'");

        btnSearch.Attributes.Add("onmouseover", "this.style.backgroundColor='#00B5F0'");
        btnSearch.Attributes.Add("onmouseout", "this.style.backgroundColor='#008FBF'");

        btnDeletePermission.Attributes.Add("OnClick", "confirmDelete()");
        if (!IsPostBack)
        {
            Session["UserManager_PermissionSearch"] = "Latest";
            SearchPermission();
            upnlAddPermission.Visible = false;
        }
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        if (txtSearch.Text == string.Empty)
            Session["UserManager_PermissionSearch"] = "BlankSearch";
        else
            Session["UserManager_PermissionSearch"] = txtSearch.Text;
        SearchPermission();   
    }

    protected void btnAddPermission_Click(object sender, EventArgs e)
    {
        Session["UserManager_Permission"] = "Permission Add";
        upnlPermissions.Visible = false;
        upnlAddPermission.Visible = true;
        btnSave.Text = "Save";
        //Add();
        Clear();
    }

    protected void gvwPermission_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvwPermissions.PageIndex = e.NewPageIndex;
        SearchPermission ();

    }

    protected void lnbHome_Click(object sender, EventArgs e)
    {
        Response.Redirect("BusinessProcessAutomation.aspx");
    }

    protected void btnAssign_Click(object sender, EventArgs e)
    {
        Session["UserManager_Control"] = "UserManager/ucActorPermissionSearch.ascx";
        Response.Redirect("BusinessProcessAutomation.aspx");
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        Add();
        Clear();
    }

    protected void btnBack_Click(object sender, EventArgs e)
    {
        SearchPermission();
        upnlAddPermission.Visible = false;
        upnlPermissions.Visible = true;        
    }

    protected void btnDeletePermission_Click(object sender, EventArgs e)
    {
        DeletePermission();
    }

    protected void btnEditPermission_Click(object sender, EventArgs e)
    {
        if (CheckSelection())
        {
            Clear();
            Session["UserManager_Permission"] = "Permission Edit";
            upnlPermissions.Visible = false;
            upnlAddPermission.Visible = true;
            btnSave.Text = "Update";
            LoadData();
        }
    }
    
    protected void gvwPermissions_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvwPermissions.PageIndex = e.NewPageIndex;
        SearchPermission();
    }

    protected void gvwPermissions_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {

            e.Row.Attributes.Add("onmouseover", "MouseEvents(this, event)");

            e.Row.Attributes.Add("onmouseout", "MouseEvents(this, event)");

        } 

    }

    #endregion

    #region Methods
    /// <summary>
    /// 
    /// </summary>
    /// <returns></returns>
    bool CheckSelection()
    {
        int count = 0, selectedRow = -1;

        for (int i = 0; i < gvwPermissions.Rows.Count; i++)
        {
            CheckBox chkGrid = (CheckBox)(gvwPermissions.Rows[i].Cells[0].FindControl("chkEditDelete"));
            if (chkGrid.Checked == true)
            {
                count++;
                selectedRow = i;
            }
        }

        if (count == 0)
        {
            Response.Write("<script>alert('Select a Permission')</script>");
            return false;
        }
        else if (count > 1)
        {
            Response.Write("<script>alert('Select Single Permission')</script>");
            return false;
        }

        Session["UserManager_PermissionID"] = gvwPermissions.Rows[selectedRow].Cells[1].Text;
        return true;
    }

    /// <summary>
    /// 
    /// </summary>
    void LoadData()
    {
        string condition = "PL_PermissionID = " + Session["UserManager_PermissionID"] + " ";
        DataSet dsUser = _userPermission.GetData("PermissionList", condition);

        txtPermissionName.Text = dsUser.Tables[0].Rows[0]["PL_PermissionName"].ToString();
        txtCustomID.Text = dsUser.Tables[0].Rows[0]["PL_CustomID"].ToString();
    }

    /// <summary>
    /// Serch Permission
    /// </summary>
    void SearchPermission()
    {
        try
        {
            DataSet ds = new DataSet();

            if (!_userPermission.PermissionSearch(Session["UserManager_PermissionSearch"].ToString(), out ds))
            {
                Response.Write("<script>alert('No Match Found !')</script>");
            }

            gvwPermissions.Visible = true;
            gvwPermissions.DataSource = ds;
            gvwPermissions.Columns[1].Visible = true;
            gvwPermissions.DataBind();
            gvwPermissions.Columns[1].Visible = false;
        }
        catch (Exception exp)
        {
            Session["ERRTITLE"] = "Search Permission Failed";
            Session["ERRDET"] = exp.Message;
            Response.Redirect("ErrorPage.aspx");
        }
    }

    /// <summary>
    /// Clear all Controls
    /// </summary>
    void Clear()
    {
        txtPermissionName.Text = string.Empty;
        txtCustomID.Text = string.Empty;
    }

    /// <summary>
    /// Add Permissions
    /// </summary>
    void Add()
    {
        try
        {
            UserPermission editPermission;
            if (Session["UserManager_Permission"] == "Permission Add")
            {
                if (txtPermissionName.Text == string.Empty)
                {
                    Response.Write("<script>alert('Enter Permission Name')</script>");
                    return;
                }
                if ((txtCustomID.Text != string.Empty) && (!_userPermission.CheckNumber(txtCustomID.Text)))
                {
                    Response.Write("<script>alert('CustomID Should Be Numeric')</script>");
                    return;
                }

                string permissionName = txtPermissionName.Text;
                string customID = txtCustomID.Text;
                if (!_userPermission.AddPermission(permissionName, customID))
                {
                    Response.Write("<script>alert('Permission Already Exist')</script>");
                }
                else
                    Response.Write("<script>alert('Added Successfully !')</script>");
            }
            else
            {
                editPermission = new UserPermission();
                editPermission.PermissionName = txtPermissionName.Text;
                editPermission.CustomID = txtCustomID.Text;
                editPermission.PermissionID = Session["UserManager_PermissionID"].ToString();
                _userPermission.EditPermission(editPermission);
            }
        }
        catch (Exception exp)
        {
            Session["ERRTITLE"] = "Add/Edit Permission Failed";
            Session["ERRDET"] = exp.Message;
            Response.Redirect("ErrorPage.aspx");
        }
    }

    /// <summary>
    /// Delete Prmission
    /// </summary>
    void DeletePermission()
    {
        int index = 0;
        string permissionID;
        string[] permissionIDs = new string[gvwPermissions.Rows.Count];
        for (int i = 0; i < gvwPermissions.Rows.Count; i++)
        {
            CheckBox chkGrid = (CheckBox)(gvwPermissions.Rows[i].Cells[0].FindControl("chkEditDelete"));
            if (chkGrid.Checked == true)
            {
                permissionIDs[index] = gvwPermissions.Rows[i].Cells[1].Text;
                index++;
            }
        }

        if (index == 0)
        {
            Response.Write("<script>alert('Select Permission')</script>");
            return;
        }
        
        string conditionDeletePermission = "PL_PermissionID='" + permissionIDs[0] + "'";
        string conditionDeletePermissionMap="PM_PermissionID ='" + permissionIDs[0] + "'";

        for (int i = 0; i < index; i++)
        {
            conditionDeletePermission = conditionDeletePermission + "or PL_PermissionID='" + permissionIDs[i] + "'";
            conditionDeletePermissionMap = conditionDeletePermissionMap + "or PM_PermissionID='" + permissionIDs[i] + "'";
        }
        _userPermission.PermissionDelete(conditionDeletePermission, conditionDeletePermissionMap);

        Response.Write("<script>alert('Deleted Successfully !')</script>");
        SearchPermission();
    }
    #endregion

    protected void gvwPermissions_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
}
